package com.easylinkin.linkappapi.openapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.mapper.DeviceMapper;
import com.easylinkin.linkappapi.openapi.service.ApplicationCopyDeviceService;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import com.easylinkin.linkappapi.space.entity.LinkappArea;
import com.easylinkin.linkappapi.space.mapper.LinkappAreaMapper;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group-cloud
 * @description: 拷贝设备业务数据
 * @author: chenkaixuan
 * @create: 2021-09-09 15:38
 */
@Slf4j
@Order(2)
@Service
public class ApplicationCopyBusinessDataDeviceService implements ApplicationCopyDeviceService {
    @Resource
    DeviceMapper deviceMapper;
    @Resource
    LinkappAreaMapper linkappAreaMapper;

    public static void main(String[] args) {
        String s=null;
        System.out.println(JSONObject.toJSONString(s));
    }

    @Override
    public RestMessage copy(HashMap<String, String> screenmap, HashMap<String, String> map, LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        log.info("进入拷贝设备业务数据screenmap:"+JSONObject.toJSONString(screenmap)+", deviceMap:"+JSONObject.toJSONString(map)
        +", newLinkappUser:"+JSONObject.toJSONString(newLinkappUser)+", oldLinkappUser:"+JSONObject.toJSONString(oldLinkappUser));
        if (newLinkappUser == null || oldLinkappUser == null) {
            return RestBuilders.failureMessage().setMessage("用户为空");
        }
        RestMessage restMessage = RestBuilders.successBuilder().build();
        if (map == null || map.size() <= 0) {
            return restMessage;
        }
        //获取旧设备
        Device device = new Device();
        List<Device> oldDeviceList = deviceMapper.selectDevicesByCodes(map.keySet(),device);
        if(oldDeviceList == null || oldDeviceList.size()<=0){
            log.info("拷贝设备业务数据获取旧设备为空，提前退出，param:"+JSONObject.toJSONString(map.keySet()));
            return restMessage;
        }
        Set<String> oldAreaPaths = oldDeviceList.stream().filter(m -> !StringUtils.isEmpty(m.getAreaPath())).map(Device::getAreaPath).collect(Collectors.toSet());
        //获取新区域
        Map<String, LinkappArea> newAreaMap=null;
        if(oldAreaPaths != null &&oldAreaPaths.size()>0){
            LinkappArea linkappArea = new LinkappArea();
            linkappArea.setTenantId(newLinkappUser.getTenantId());
            linkappArea.setAreaPaths(new ArrayList<>(oldAreaPaths));
            List<LinkappArea> linkappAreaList = linkappAreaMapper.selectLinkappAreaListAllGlobal(linkappArea);
            if(linkappAreaList != null && linkappAreaList.size()>0){
                newAreaMap = linkappAreaList.stream().collect(Collectors.toMap(LinkappArea::getAreaPath, m -> m, (k1, k2) -> k2));
            }
        }
        //获取新设备
        device=new Device();
        List<Device> newDeviceList = deviceMapper.selectDevicesByCodes(new HashSet<>(map.values()), device);
        if(newDeviceList == null || newDeviceList.size()<=0){
            log.info("拷贝设备业务数据获取新设备为空，提前退出，param:"+JSONObject.toJSONString(map.values()));
            return restMessage;
        }
        //旧devicecode和新devicecode位置互换
        Map<String, String> deviceMap = map.entrySet().stream().collect(Collectors.toMap(m -> m.getValue(), m -> m.getKey(), (k1, k2) -> k2));
        Map<String, Device> oldDeviceMap = oldDeviceList.stream().collect(Collectors.toMap(m -> m.getCode(), m -> m, (k1, k2) -> k2));
        Map<String, LinkappArea> finalNewAreaMap = newAreaMap;
        Date date = new Date();
        newDeviceList=newDeviceList.stream().map(m->{
            m.setModifyTime(date);
            String oldDeviceCode = deviceMap.get(m.getCode());
            if(oldDeviceCode == null){
                return m;
            }
            Device oldDevice = oldDeviceMap.get(oldDeviceCode);
            if(oldDevice == null){
                return m;
            }
            if(finalNewAreaMap == null || finalNewAreaMap.size()<=0 || finalNewAreaMap.get(oldDevice.getAreaPath()) == null){
                return m;
            }
            LinkappArea newlinkappArea = finalNewAreaMap.get(oldDevice.getAreaPath());
            m.setAreaId(newlinkappArea.getId());
            m.setLatitude(oldDevice.getLatitude());
            m.setLongitude(oldDevice.getLongitude());
            m.setIndoorLocation(oldDevice.getIndoorLocation());
            m.setSite(oldDevice.getSite());
            m.setAreaPath(newlinkappArea.getAreaPath());
            return m;
        }).collect(Collectors.toList());
        int batchSize = 100;
        if (newDeviceList != null && newDeviceList.size() > 0) {
            log.info("拷贝设备业务数据修改参数list:"+JSONObject.toJSONString(newDeviceList));
            for (Device inertdevice : newDeviceList) {
                deviceMapper.updateDeviceAreaById(inertdevice);
            }
        }
        log.info("拷贝设备业务数据成功退出");
        return restMessage;
    }

    @Override
    public RestMessage copy(LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        return null;
    }
}
